package cqrtplm.util;

import com.hustcad.plm.basic.log.LogEventEnum;
import com.hustcad.plm.basic.log.TyplmLogger;
import com.hustcad.plm.pdm.user.service.TyplmUserService;
import com.ty.basic.entity.audit.AuditLogDO;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class RTLogHelper {
    @Resource
    private TyplmUserService typlmUserService;
    @Resource
    private TyplmLogger typlmLogger;
    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    public void insertIntoLog(String input, String output,String userName) {
        UserDO userDO = typlmUserService.queryUserByName(userName);
        if(userDO == null){
            userDO = typlmUserService.queryUserByName("admin");
        }
        AuditLogDO logDO = new AuditLogDO();
        logDO.setOid(snowflakeIdComponent.getInstance().nextId());
        logDO.setCreatestamp(new Date());
        logDO.setCreatoroid(userDO.getOid());
        logDO.setCreatorotype(userDO.getOtype());
        logDO.setDescription(output);
        logDO.setTargetidentity(input);
        logDO.setErrorfalg(0);
        this.typlmLogger.info(logDO, LogEventEnum.GETDOCLIST);
    }
}
